<?php

namespace seraph_pds;

// Exit if accessed directly
if( !defined( 'ABSPATH' ) )
	exit;

require_once( 'Cmn/Gen.Php' );
require_once( 'Cmn/Ui.Php' );
require_once( 'Cmn/Plugin.Php' );
require_once( 'Cmn/Fs.Php' );

// #######################################################################

const PLUGIN_SETT_VER								= 1;
const PLUGIN_DATA_VER								= 1;
const PLUGIN_EULA_VER								= 1;
const PLUGIN_SETT_PAGE_ID							= 'seraph_pds_settings';

// #######################################################################
// #######################################################################

function _GetPostImagesUrl( $postFrom )
{
	$site_url = site_url();

/*
	if( has_post_thumbnail( $postFrom -> ID ) )
    {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $postFrom -> ID ), 'single-post-thumbnail' );
        $post_img_dir = dirname( $image[ 0 ] );
    }
    else
*/
	{
		global $post, $post_id;

		$post_prev = $post;
		$post = null;
		$post_id_prev = $post_id;
		$post_id = $postFrom -> ID;

		$file = apply_filters( 'wp_handle_upload_prefilter', array( 'name' => "dummy.jpg", 'ext'  => "jpg", 'type' => "jpg" ) );

		$wp_upload_dir_res = wp_upload_dir( null, false );
		$post_img_dir = $wp_upload_dir_res[ 'url' ];

		$fileinfo = apply_filters( 'wp_handle_upload', array( 'file' => $file[ 'name' ], 'url'  => $post_img_dir . "/" . $file[ 'name' ], 'type' => $file[ 'type' ] ), 'upload' );

		$post = $post_prev;
		$post_id = $post_id_prev;
	}

	if( strpos( $post_img_dir, $site_url ) === 0 )
		$post_img_dir = substr( $post_img_dir, strlen( $site_url ), strlen( $post_img_dir ) - strlen( $site_url ) );

	return $post_img_dir;
}

function _GetAttachmentIdFromUrl( $attachment_url = '' )
{
	global $wpdb;
	$attachment_id = 0;

	// If there is no url, return.
	if( '' == $attachment_url )
		return( null );

	// Get the upload directory paths
	$upload_dir_paths = wp_upload_dir();

	// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
	if( false !== strpos( $attachment_url, $upload_dir_paths[ 'baseurl' ] ) )
	{
		// If this is the URL of an auto-generated thumbnail, get the URL of the original image
		$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

		// Remove the upload path base directory from the attachment URL
		$attachment_url = str_replace( $upload_dir_paths[ 'baseurl' ] . '/', '', $attachment_url );

		// Finally, run a custom database query to get the attachment ID from the modified attachment URL
		$attachment_id = $wpdb -> get_var( $wpdb -> prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
	}

	return( $attachment_id );
}

const POSTS_TAXONOMY_META_CATEGORIES					= 'post_categories_meta_box';
const POSTS_TAXONOMY_META_TAGS							= 'post_tags_meta_box';

function GetPostsTaxonomies( $taxonomyMeta )
{
	$taxonomies = get_taxonomies( NULL, 'objects' );
	if( !is_array( $taxonomies ) )
		return( array() );

	$res = array();

	foreach( $taxonomies as $taxonomyId => $taxonomy )
	{
		if( !$taxonomy -> show_ui )
			continue;

		if( $taxonomy -> meta_box_cb != $taxonomyMeta )
			continue;

		foreach( $taxonomy -> object_type as $taxonomyPostType )
			$res[ $taxonomyPostType ] = $taxonomyId;
	}

	return( $res );
}

function GetPostsAvailableCategories( $postTypes = NULL )
{
	$mapPostTypeToCatTaxonomy = GetPostsTaxonomies( POSTS_TAXONOMY_META_CATEGORIES );

	$cats = array();

	if( empty( $postTypes ) )
		$postTypes = GetCompatiblePostsTypes();

	foreach( $postTypes as $postType )
	{
		$postCatTaxonomy = isset( $mapPostTypeToCatTaxonomy[ $postType ] ) ? $mapPostTypeToCatTaxonomy[ $postType ] : NULL;
		if( empty( $postCatTaxonomy ) )
			continue;

		$postCats = get_terms( $postCatTaxonomy, array( 'hide_empty' => false ) );
		if( empty( $postCats ) )
			continue;

		$postCatsNew = array();
		foreach( $postCats as $postCat )
		{
			$postCatNew = array( 'slug' => $postCat -> slug, 'name' => $postCat -> name, 'parent' => $postCat -> parent );
			$postCatsNew[ $postCat -> term_id ] = $postCatNew;
		}

		$cats[ $postType ] = $postCatsNew;
	}


	return( $cats );
}

function GetCompatiblePostsTypes()
{
	$res = array();

	$postTypes = get_post_types();
	foreach( $postTypes as $postType )
	{
		if( !post_type_supports( $postType, 'editor' ) )
			continue;
		if( !post_type_supports( $postType, 'thumbnail' ) )
			continue;

		$res[] = $postType;
	}

	return( $res );
}

// #######################################################################

const ShowOperateOptionsMode_Post		= 1;
const ShowOperateOptionsMode_Batch		= 2;
const ShowOperateOptionsMode_Settings	= 3;

function _post_is_new( $post )
{
	return( empty( $post -> post_title ) );
}

function _ShowOperateOptions( $mode, $post = NULL )
{
	$rmtCfg = PluginRmtCfg::Get();
	$sett = Plugin::SettGet();

	$postIsNew = empty( $post ) ? false : _post_is_new( $post );

	$aPostTypes = array();
	if( $mode != ShowOperateOptionsMode_Post )
		$aPostTypes = GetCompatiblePostsTypes();

	//<tr valign="top">
	//    <th scope="row">
	//        External media files:
	//    </th>
	//    <td>
	//        <fieldset>
	//            <input type="file" id="seraph_pds_upload-external-media-files-helper" multiple></input>
	//        </fieldset>
	//    </td>
	//</tr>

	?>

	<?php if( $mode != ShowOperateOptionsMode_Settings ) { ?>
		<input type="hidden" id="seraph_pds_site_name" value="<?php echo( Gen::GetSiteDisplayName() ); ?>" />
		<input type="hidden" id="seraph_pds_upload-image-nonce" value="<?php echo( wp_create_nonce( "media-form" ) ); ?>" />
		<input type="hidden" id="seraph_pds_upload-image-request-url" value="<?php echo( get_admin_url( NULL, 'async-upload.php' ) ); ?>" />
		<input type="hidden" id="seraph_pds_helper_request_url" value="<?php echo( get_admin_url( NULL, 'admin.php' ) ); ?>" />
		<input type="hidden" id="seraph_pds_posts_avail_cats" value="<?php echo( urlencode( wp_json_encode( GetPostsAvailableCategories( empty( $post ) ? NULL : array( $post -> post_type ) ) ) ) ); ?>" />
		<?php if( $mode == ShowOperateOptionsMode_Post ) { ?>
			<input type="hidden" id="seraph_pds_postType" value="<?php echo( $post -> post_type ); ?>" />
			<input type="hidden" id="seraph_pds_upload-post-url" value="<?php echo( get_permalink( $post ) ); ?>" />
			<input type="hidden" id="seraph_pds_avail_plugins" value="<?php echo( urlencode( wp_json_encode( Plugin::GetAvailablePlugins() ) ) ); ?>" />
		<?php } ?>
		<?php if( $mode == ShowOperateOptionsMode_Batch ) { ?>
			<?php _UploadPage_AddParams(); ?>
		<?php } ?>
	<?php } ?>
            
	<table class="<?php echo( ( $mode == ShowOperateOptionsMode_Settings ) ? 'form-table' : 'form-table seraph_pds_table' ); ?>"><tbody>
		<?php if( $mode == ShowOperateOptionsMode_Post ) { ?>
			<tr valign="top">
				<th scope="row">
					Source file<?php echo( Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.InplaceFileSource' ), Ui::AdminHelpBtnModeText ) ); ?>
				</th>
				<td>
					<fieldset>
						<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
							<tbody>
								<tr>
									<td style="width: 1px; padding-right: 0.25em;">
										<input style="display: none;" type="file" accept=".docx" id="seraph_pds_upload" onchange="if( seraph_pds_upload.files.length ) seraph_pds_filename.value = seraph_pds_upload.files[ 0 ].name;" />
										<input type="button" class="button" value="Browse..." onclick="seraph_pds_upload.click();" />
									</td>
									<td>
										<input style="width: 100%" id="seraph_pds_filename" type="text" readonly />
									</td>
								</tr>
							</tbody>
						</table>
					</fieldset>
				</td>
			</tr>
		<?php } ?>

		<?php if( $mode == ShowOperateOptionsMode_Batch || $mode == ShowOperateOptionsMode_Settings ) { ?>
			<tr valign="top">
				<th scope="row">
					<?php echo( $mode == ShowOperateOptionsMode_Settings ? 'Content Types' : 'Source file(s)' ); ?><?php if( $mode == ShowOperateOptionsMode_Batch ) echo( Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.DirectFileSources' ), Ui::AdminHelpBtnModeText ) ); ?>
				</th>
				<td>
					<fieldset>
						<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
							<tbody>
								<tr>
									<td>
										<select id="seraph_pds_postType" onchange="seraph_pds_postType_Set( this.selectedIndex >= 0 ? this.options[ this.selectedIndex ].value : '' );">
											<?php

											foreach( $aPostTypes as $postType )
												echo( '<option value="' . $postType . '"' . ( $postType == 'post' ? ' selected' : '' ) . '>' . get_post_type_object( $postType ) -> labels -> name . '</option>' );

											?>
										</select>
										<script>
											function seraph_pds_postType_Set( v )
											{
												jQuery( ".seraph_pds_settBlock" ).hide();
												jQuery( "#seraph_pds_settBlock_" + v ).show();
											}
										</script>
											
										<?php if( $mode == ShowOperateOptionsMode_Batch ) { ?>
											<input style="display: none;" type="file" accept=".docx" multiple id="seraph_pds_upload" onchange="" />
											<input type="button" class="button" value="Browse..." onclick="seraph_pds_upload.click();" />
										<?php } ?>
									</td>
								</tr>
								<tr>
									<td>
										<?php if( $mode == ShowOperateOptionsMode_Batch ) { ?>
											<textarea style="width: 100%; min-height: 3em; max-height: 30em;" id="seraph_pds_filenames" type="text" readonly></textarea>
										<?php } ?>
									</td>
								</tr>
							</tbody>
						</table>
							
					</fieldset>
				</td>
			</tr>

		<?php } ?>
	</tbody></table>

	<?php

	if( $mode == ShowOperateOptionsMode_Post )
		_ShowOperateOptionsBlockForPostType( $mode, $post -> post_type, $postIsNew, $rmtCfg, $sett, $post );
	else
	{
		foreach( $aPostTypes as $postType )
			_ShowOperateOptionsBlockForPostType( $mode, $postType, $postIsNew, $rmtCfg, $sett );

		?>
		<script>
			seraph_pds_postType_Set( "<?php echo( empty( $aPostTypes ) ? '' : $aPostTypes[ 0 ] ); ?>" );
		</script>
		<?php
	}

	?>
				
	<?php if( $mode == ShowOperateOptionsMode_Post ) { ?>
		<table class="form-table seraph_pds_table"><tbody>
			<tr valign="top">
				<th scope="row">
					Media URL base:<?php echo( Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.MediaURLBase' ), Ui::AdminHelpBtnModeText ) ); ?>
				</th>
				<td>
					<fieldset>
						<input id="seraph_pds_post-media-url" type="text" value="<?php echo( _GetPostImagesUrl( $post ) ); ?>" />
						<p class="description">URLs of not uploaded medias will be generated according to 'Media URL base'.</p>
					</fieldset>
				</td>
			</tr>
		</tbody></table>
	<?php } ?>
	
	<?php if( $mode != ShowOperateOptionsMode_Settings ) { ?>
		<p>
			<input type="button" class="<?php echo( $mode == ShowOperateOptionsMode_Post ? 'button' : 'button-primary' ); ?>" id="seraph_pds_reload" value="<?php echo( $mode == ShowOperateOptionsMode_Post ? 'Update' : 'Add/Update' ); ?>" />
			<span id="seraph_pds_loading" class="spinner"></span>
		</p>

		<div>
			<strong>Log:</strong>
			<textarea id="seraph_pds_messages" readonly style="min-height: <?php echo( $mode == ShowOperateOptionsMode_Post ? '7' : '14' ); ?>em;"></textarea>
		</div>
	<?php } ?>
	
	<?php
}

function _ShowOperateOptionsBlockForPostType( $mode, $postType, $postIsNew, $rmtCfg, $sett, $post = NULL )
{
	?>

	<table id="<?php echo( 'seraph_pds_settBlock_' . $postType ); ?>" class="seraph_pds_settBlock <?php echo( ( $mode == ShowOperateOptionsMode_Settings ) ? 'form-table' : 'form-table seraph_pds_table' ); ?>"><tbody>
		<tr valign="top">
			<th scope="row">
				Document binding
			</th>
			<td>
				<fieldset>
					<?php $fldId = 'docTypes/' . $postType . '/useSubjectAsID'; ?>
					<?php echo( Ui::CheckBox( 'Bind document to content' . Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.PostIdBinding' ), Ui::AdminHelpBtnModeChkRad ), 'seraph_pds' . '/' . $fldId, Gen::GetArrField( $sett, $fldId, false, '/' ), $mode == ShowOperateOptionsMode_Settings ) ); ?>
				</fieldset>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row">
				Non-existent hyperlinks<?php echo( Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.NonExistentLinks' ), Ui::AdminHelpBtnModeText ) ); ?>
			</th>
			<td>
				<fieldset>
					<table border="0" cellpadding="0" cellspacing="0">
						<tbody>
							<?php $fldId = 'docTypes/' . $postType . '/notexistLinksAction'; $groupName = 'seraph_pds' . '/' . $fldId; $fldVal = Gen::GetArrField( $sett, $fldId, 'Underline', '/' ); ?>
							<tr>
								<td>
									<?php echo( Ui::RadioBox( 'Underline', $groupName, 'Underline', $fldVal == 'Underline' ) ); ?>
								</td>
								<td>
									<?php echo( Ui::RadioBox( 'Check only', $groupName, 'CheckOnly', $fldVal == 'CheckOnly' ) ); ?>
								</td>
							</tr>
							<tr>
								<td>
									<?php echo( Ui::RadioBox( 'Remove', $groupName, 'Del', $fldVal == 'Del' ) ); ?>
								</td>
								<td>
									<?php echo( Ui::RadioBox( 'Don\'t check', $groupName, 'No', $fldVal == 'No' ) ); ?>
								</td>
							</tr>
						</tbody>
					</table>
				</fieldset>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row">
				Attributes
			</th>
			<td>
				<fieldset>
					<table border="0" cellpadding="0" cellspacing="0">
						<tbody>
							<tr>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useTitle';

									$fldSubId = 'docTypes/' . $postType . '/useTitle_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Body', '/' );

									echo( Ui::CheckBox(
										array(
											'Title ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'title\'',			$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'title\'',			$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'title\'',			$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Title' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );
									?>
								</td>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useTags';

									$fldSubId = 'docTypes/' . $postType . '/useTags_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Prop,Body', '/' );

									echo( Ui::CheckBox(
										array(
											'Tags ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'tags\'',			$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'tags\'',			$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'tags\'',			$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Tags' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useSeoTitle';

									$fldSubId = 'docTypes/' . $postType . '/useSeoTitle_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Prop,Body', '/' );

									echo( Ui::CheckBox(
										array(
											'SEO title ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'title\'',			$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'title\'',			$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'title\'',			$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.SeoTitle' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
							</tr>
							<tr>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useExcerpt';

									$fldSubId = 'docTypes/' . $postType . '/useExcerpt_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Body', '/' );

									echo( Ui::CheckBox(
										array(
											'Excerpt ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'comments\'',		$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'comments\'',		$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'comments\'',		$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Excerpt' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useCategories';

									$fldSubId = 'docTypes/' . $postType . '/useCategories_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Prop,Body', '/' );

									echo( Ui::CheckBox(
										array(
											'Categories ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'categories\'',		$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'categories\'',		$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'categories\'',		$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Categories' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useSeoDescription';

									$fldSubId = 'docTypes/' . $postType . '/useSeoDescription_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Prop,Body', '/' );

									echo( Ui::CheckBox(
										array(
											'SEO description ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from \'comments\'',		$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from \'comments\'',		$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from \'comments\'',		$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.SeoDescription' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
							</tr>
							<tr>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useSlug';

									$fldSubId = 'docTypes/' . $postType . '/useSlug_Mode';
									$fldSubVal = Gen::GetArrField( $sett, $fldSubId, 'Body', '/' );

									echo( Ui::CheckBox(
										array(
											'Slug ',
											array(
												'seraph_pds' . '/' . $fldSubId,
												array(
													array( 'Body',			'not from filename',			$fldSubVal == 'Body' ),
													array( 'Prop,Body',		'also from filename',			$fldSubVal == 'Prop,Body' ),
													array( 'Prop',			'only from filename',			$fldSubVal == 'Prop' )
												)
											),
											Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Slug' ), Ui::AdminHelpBtnModeChkRad )
										),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/useFeaturedImage';

									echo( Ui::CheckBox(
										'Featured image' . Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.FeaturedImage' ), Ui::AdminHelpBtnModeChkRad ),
										'seraph_pds' . '/' . $fldId,
										Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
								<td></td>
							</tr>
						</tbody>
					</table>
				</fieldset>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row">
			</th>
			<td>
				<fieldset>
					<?php

					$fldId = 'docTypes/' . $postType . '/useInplaceAttrs';

					echo( Ui::CheckBox(
							'Custom attributes' . Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.OtherH1ExtAttrs' ), Ui::AdminHelpBtnModeChkRad ),
							'seraph_pds' . '/' . $fldId,
							Gen::GetArrField( $sett, $fldId, true, '/' ),
							$mode == ShowOperateOptionsMode_Settings
					) );

					?>

					<?php if( $mode == ShowOperateOptionsMode_Post ) { ?>
					<p class="description">Document content for additional <span id="<?php echo( 'seraph_pds' . '/docTypes/' . $postType . '/useInplaceAttrsText' ); ?>"></span>text block(s) must begin with bookmarked 'Header1' with the same identificator(s).</p>
					<?php } ?>
				</fieldset>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row">
				<div style="margin-right: 2em;">
					Media<?php echo( Ui::AdminHelpBtn( Gen::GetArrField( $rmtCfg, 'Help.Media' ), Ui::AdminHelpBtnModeText ) ); ?>
				</div>
			</th>
			<td>
				<fieldset>
					<table border="0" cellpadding="0" cellspacing="0">
						<tbody>
							<tr>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/uploadMedia';

									echo( Ui::CheckBox(
										'Upload',
										'seraph_pds' . '/' . $fldId,
										$postIsNew ? false : Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
							</tr>
							<tr>
								<td>
									<?php

									$fldId = 'docTypes/' . $postType . '/uploadImageCheckUrl';

									echo( Ui::CheckBox(
										'Check final URLs',
										'seraph_pds' . '/' . $fldId,
										$postIsNew ? false : Gen::GetArrField( $sett, $fldId, true, '/' ),
										$mode == ShowOperateOptionsMode_Settings
									) );

									?>
								</td>
							</tr>
						</tbody>
					</table>

					<?php if( $postIsNew ) { ?>
					<p class="description"><strong>Warning!</strong>To preserve post's media final URLs in a right way we don't recommend to upload any images until save new post at first time.</p>
					<?php } ?>
					<p class="description">Only embeded document's media can be uploaded due to browser's security limitations.</p>
				</fieldset>
			</td>
		</tr>
	</tbody></table>

	<?php
}

// #######################################################################

function OnOptRead_Data( $data, $verFrom )
{
	return( $data );
}

function OnOptWrite_Data( $data )
{
	return( $data );
}

// #######################################################################

function OnOptRead_Sett( $sett, $verFrom )
{
	return( $sett );
}

function OnOptWritePrep_Sett( $sett )
{
	$postTypes = GetCompatiblePostsTypes();

	$atLeastOneEnabled = false;
	foreach( $postTypes as $postType )
		if( Gen::GetArrField( $sett, array( 'docTypes', $postType, 'enable' ), false ) )
			$atLeastOneEnabled = true;

	if( !$atLeastOneEnabled && count( $postTypes ) )
		Gen::SetArrField( $sett, array( 'docTypes', $postTypes[ 0 ], 'enable' ), true );

	return( $sett );
}

// #######################################################################

function getUrl( $url, $body = false )
{
	/*
	{
		$arrURL = @parse_url( $url );
		if( empty( $arrURL[ 'host' ] ) )
			$url = rtrim( get_site_url(), '/' ) . '/' . ltrim( $url, '/' );
	}
	*/

	$requestRes = null;
	for( $iTime = time(); time() - $iTime < 30; )
	{
		$requestRes = $body ? wp_remote_get( $url ) : wp_remote_head( $url );
		if( !is_wp_error( $requestRes ) )
			break;
	}

	if( is_wp_error( $requestRes ) )
		return( wp_json_encode( array( 'hr' => Gen::E_FAIL/*, 'trace' => print_r( $requestRes, true )*/ ) ) );

	$httpStatus = wp_remote_retrieve_response_code( $requestRes );
	if( !( $httpStatus >= 200 && $httpStatus < 400 || $httpStatus == 403 || $httpStatus == 423 ) )
		return( wp_json_encode( array( 'hr' => Gen::S_FALSE, 'httpCode' => $httpStatus ) ) );

	$res = array( 'hr' => Gen::S_OK );
	if( $body )
		$res[ 'body' ] = wp_remote_retrieve_body( $requestRes );

	return( wp_json_encode( $res ) );
}

// #######################################################################
// #######################################################################
